import { NextApiRequest, NextApiResponse } from "next";
import { getPassCodeByEmail } from "@/lib/logupemail_database_method";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    // 设置 CORS 头部
    res.setHeader('Access-Control-Allow-Origin', '*'); // 允许所有来源
    const data = req.body;
    console.log(data);
    const sql = 'SELECT * FROM logup_email WHERE email = ?';
    const params = [data.email];

    try {
        const dbresult = await getPassCodeByEmail(sql, params);
        console.log(dbresult);
        if (dbresult.length) {
            if (dbresult[0].passcode === data.passcode) {
                res.status(200).json({ passcode: true });
            } else {
                res.status(200).json({ passcode: false });
            }
        } else {
            res.status(200).json({ passcode: false });
        }

    } catch (error) {
        console.log(error);
    }
}